<template>
  <div>
    <div
      class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted"
    >
      <div class="mui-scroll">
        <a
          :class="{ 'mui-control-item': true, 'mui-active': item.id===0 }"
          v-for="item in categorys"
          :key="item.id"
          @click="getImageListByCategory(item.id)"
        >{{ item.title }}</a>
      </div>
    </div>
    <ul class="img-list">
      <router-link tag="li" :to="'/home/photoinfo/'+item.id" v-for="item in images" :key="item.id">
        <img v-lazy="item.imgs[0]">
        <div class="info">
          <h1 class="info-title">{{ item.title }}</h1>
          <div class="info-body">&emsp;&emsp;{{ item.desc }}</div>
        </div>
      </router-link>
    </ul>
  </div>
</template>

<script>
import mui from "../../lib/mui/dist/js/mui.min.js";
import { Toast } from "mint-ui";

export default {
  data() {
    return {
      categorys: [],
      images: []
    };
  },
  created() {
    this.getCategorys();
    this.getImageListByCategory(0);
  },
  methods: {
    getCategorys() {
      this.$http.get("imageCategorys").then(
        result => {
          this.categorys = result.body;
          this.categorys.unshift({
            id: 0,
            title: "全部"
          });
        },
        error => {
          Toast("图片分类类别加载失败！");
        }
      );
    },
    getImageListByCategory(categoryid) {
      var url = "";
      if (categoryid === 0) {
        url = "images";
      } else {
        url =
          "images?categoryid_gte=" +
          categoryid +
          "&categoryid_lte=" +
          categoryid;
      }
      this.$http.get(url).then(
        result => {
          this.images = result.body;
        },
        error => {
          Toast("图片列表获取失败！");
        }
      );
    }
  },
  mounted() {
    // 页面挂载了之后，再操作dom元素
    mui(".mui-scroll-wrapper").scroll({
      deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
    });
  }
};
</script>

<style lang="scss" scoped>
* {
  touch-action: pan-y;
}

.img-list {
  list-style: none;
  margin: 0;
  padding: 10px;
  padding-bottom: 0px;
  li {
    background-color: #ccc;
    text-align: center;
    margin-bottom: 10px;
    box-shadow: 0 0 10px #999;
    position: relative;
    max-height: 400px;
    img {
      width: 100%;
      vertical-align: middle;
    }
    img[lazy="loading"] {
      width: 40px;
      height: 300px;
      margin: auto;
    }
    .info {
      color: white;
      text-align: left;
      position: absolute;
      bottom: 0;
      background-color: rgba(0, 0, 0, .4);
      width: 100%;
      .info-title {
        font-size: 13px;
      }
      .info-body {
        font-size: 11px;
      }
    }
  }
}
</style>
